﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;

namespace FrozenBubble
{
    public class Storage
    {
        private bool _sound;
        private bool _fullscreen;
        private HighScoreManager _highScores;

        public bool IsSoundEnabled 
        {
            get
            {
                return _sound;
            }
            set
            {
                _sound = value;
            }
        }

        public bool IsFullScreen 
        {
            get
            {
                return _fullscreen;
            }
            set
            {
                _fullscreen = value;
            }
        }

        public HighScoreManager HighScores
        {
            get
            {
                return _highScores;
            }
        }

        private bool _isFirstTime;
        public bool IsFirstTime
        {
            get
            {
                if (_isFirstTime)
                {
                    _isFirstTime = false;
                    return true;
                }
                else
                    return false;
            }
        }

        public void Load()
        {
            // Default values
            _sound = true;
            _fullscreen = true;
            _highScores = new HighScoreManager();
            _isFirstTime = true;

#if WINDOWS_PHONE
            IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication();
            if (ifs.FileExists("settings.xml"))
            {
                using (IsolatedStorageFileStream stream = ifs.OpenFile("settings.xml", FileMode.Open))
                {
                    try
                    {
                        XDocument xdoc = XDocument.Load(stream);
                        XElement settings = xdoc.Element("settings");
                        XElement soundElement = settings.Element("sound");
                        XElement fullscreenElement = settings.Element("fullscreen");
                        XElement firstTimeElement = settings.Element("firsttime");
                        XElement highScoresElement = settings.Element("highscores");

                        if (soundElement != null)
                            bool.TryParse(soundElement.Value, out _sound);

                        if (fullscreenElement != null)
                            bool.TryParse(fullscreenElement.Value, out _fullscreen);

                        if (firstTimeElement != null)
                            bool.TryParse(firstTimeElement.Value, out _isFirstTime);

                        if (highScoresElement != null)
                            _highScores = new HighScoreManager(highScoresElement);
                    }
                    catch (Exception) { }
                }
            }
#else
            try
            {
                XDocument xdoc = XDocument.Load("settings.xml");
                XElement settings = xdoc.Element("settings");
                XElement soundElement = settings.Element("sound");
                XElement fullscreenElement = settings.Element("fullscreen");
                XElement highScoresElement = settings.Element("highscores");

                if(soundElement != null)
                    bool.TryParse(soundElement.Value, out _sound);

                if(fullscreenElement != null)
                    bool.TryParse(fullscreenElement.Value, out _fullscreen);

                if (highScoresElement != null)
                    _highScores = new HighScoreManager(highScoresElement);
            }
            catch(Exception) { }
#endif
        }

        public void Save()
        {
            XElement settings = new XElement("settings");
            settings.Add(new XElement("sound", _sound));
            settings.Add(new XElement("fullscreen", _fullscreen));
            settings.Add(new XElement("firsttime", _isFirstTime));
            settings.Add(_highScores.Serialize());

            XDocument xdoc = new XDocument(settings);

#if WINDOWS_PHONE
            IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = ifs.CreateFile("settings.xml"))
            {
                xdoc.Save(stream);
            }
#else
            xdoc.Save("settings.xml");
#endif
        }
    }
}
